package xiao.ke.rocket.controller;/**
 * @author XuXiaoK
 * @create 2019-11-27 22:16
 */

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xiao.ke.rocket.domain.JsonData;
import xiao.ke.rocket.jms.MsgProducer;

import java.io.UnsupportedEncodingException;

/**
 *@Author XiaoKe
 *@Date 2019/11/27 22:16
 *@Description TODO
 */
@RestController
@RequestMapping("api/v1")
public class OrderController {

    @Autowired
    private MsgProducer msgProducer;

    @GetMapping("order")
    public Object order(String msg, String tag) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {
        Message message = new Message("testTopic", tag, msg.getBytes(RemotingHelper.DEFAULT_CHARSET));
        SendResult result = msgProducer.getProducer().send(message);
        System.out.println("发送响应：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());

        return JsonData.buildSuccess();
    }
}
